package dashboard.controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import dashboard.model.RealTimeStatResult;
import dashboard.service.DatabaseService;
import dashboard.service.DebugDbCommandService;
import dashboard.service.RealTimeDbStatService;
import dashboard.service.SnapshotReportService;

@Controller
public class HelloController{

	protected Log logger = LogFactory.getLog(getClass());
	
	@Autowired
	protected DatabaseService dbService;
	@Autowired
	protected RealTimeDbStatService realTimeDbStatService;
	@Autowired
	protected DebugDbCommandService debugDbCommandService;
	@Autowired
	protected SnapshotReportService snapshotReportService;
	
	/**
	 * This is the hello page controller method
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "hello", method = RequestMethod.GET)
	public ModelAndView ww(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

       logger.info("Returning hello view");
       System.out.println("in ww() method...");
       
       ModelAndView view = new ModelAndView("hellopage", "now", "");
       
       //--------------------------------------------------------------------------
       
       //--------------------------------------------------------------------------
       RealTimeStatResult shared_pool = realTimeDbStatService.getSharedPoolStat();
       RealTimeStatResult buffer_cache = realTimeDbStatService.getBufferCacheStat();
       RealTimeStatResult redo_log_buffer = realTimeDbStatService.getRedoLogBufferStat();
       RealTimeStatResult sort_mem = realTimeDbStatService.getSortMemStat();

       System.out.println(shared_pool.getDescription() + ": " + shared_pool.getIndicator());
       view.addObject("shared_pool", shared_pool.getContent());
       
       System.out.println(buffer_cache.getDescription() + ": " + buffer_cache.getIndicator());
       view.addObject("buffer_cache", buffer_cache.getContent());
       
       System.out.println(redo_log_buffer.getDescription() + ": " + redo_log_buffer.getIndicator());
       view.addObject("redo_log_buffer", redo_log_buffer.getContent());
       
       System.out.println(sort_mem.getDescription() + ": " + sort_mem.getIndicator());
       view.addObject("sort_mem", sort_mem.getContent());
       
       //--------------------------------------------------------------------------
       String debug_user_sql_command = debugDbCommandService.debugUserInputCommandHtml("select  i.instance_number from v$database d,v$instance i");
       view.addObject("debug_user_sql_command", debug_user_sql_command);
       
       //--------------------------------------------------------------------------
       snapshotReportService.test();
       
       //--------------------------------------------------------------------------
       	Calendar date = Calendar.getInstance();
		date.add(Calendar.HOUR_OF_DAY, -2);
		Timestamp beginTime = new Timestamp(date.getTimeInMillis());
				
		//date.add(Calendar.HOUR_OF_DAY, 1);
		date.add(Calendar.MINUTE, 100);
		Timestamp endTime = new Timestamp(date.getTimeInMillis());
		
		int interval_sec = 20 * 60;
		
//		SnapshotStatResult ssr = snapshotReportService.getRatio(beginTime, endTime, interval_sec, Constant.BUFFER_CACHE);
//		
//		view.addObject("snapshot_result", ssr);
		
		return view;
	}
}
